package com.uniledger.exchange.contact;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.rohit.recycleritemclicksupport.RecyclerItemClickSupport;
import com.uniledger.exchange.R;
import com.uniledger.exchange.home.scheme.TransferActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class SearchContactFragment extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    @BindView(R.id.recy)
    RecyclerView recyclerView;

    @BindView(R.id.tv_no_result)
    TextView noResult;

    SearchAdapter searchAdapter;
    private String queryText;
    private List<UniContact> contacts;

    public SearchContactFragment() {
        // Required empty public constructor
    }


    public static SearchContactFragment newInstance(String param1, String param2) {
        SearchContactFragment fragment = new SearchContactFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_search_contact, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    public void bindDatas(List<UniContact> datas) {
        contacts = datas;
        searchAdapter = new SearchAdapter(getActivity(), datas);
        searchAdapter.setNoResult(noResult);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(searchAdapter);
        if (queryText != null) {
            searchAdapter.getFilter().filter(queryText);
        }

        RecyclerItemClickSupport.addTo(recyclerView).setOnItemClickListener(new RecyclerItemClickSupport.OnItemClickListener() {
            @Override
            public void onItemClicked(RecyclerView recyclerView, int position, View v) {
                UniContact contact = searchAdapter.getItemData(position);

            }
        });
    }

    /**
     * 根据newText 进行查找, 显示
     */
    public void bindQueryText(String newText) {
        if (contacts == null) {
            queryText = newText.toLowerCase();
        } else if (!TextUtils.isEmpty(newText)) {
            searchAdapter.getFilter().filter(newText.toLowerCase());
        }
    }


}
